package com.example.controller;

import com.example.pojo.Book;
import com.example.pojo.Borrow;
import com.example.service.BorrowService;
import com.example.service.GuestService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 名称BorrowController
 * 描述
 *
 * @version 1.0
 * @author:wanghao
 * @datatime:2023,04,20,12,25
 */
@Controller
public class BorrowController {
    @Autowired
    BorrowService borrowService;

    @Autowired
    GuestService guestService;

    @RequestMapping("BorrowBookPage")
    public String BorrowBookPage(){return "BorrowBookPage";}

    @RequestMapping("BorrowBook")
    @ResponseBody
    public String BorrowBook(Borrow borrow){
        if (guestService.FindBookByID(borrow.getBookID()).getBookNumber()>0)
        {
            int i = borrowService.InsertBorrow(borrow);
            if (i>0)
            {
                int j = borrowService.UpdateBookNumber(guestService.FindBookByID(borrow.getBookID()).getNumber()-1);
                if (j>0)    return "borrow Success";
                else return "something wrong";
            }
            else return "something wrong";
        }
        else return "no book to borrow";
    }

    @RequestMapping("ReturnBook")
    @ResponseBody
    public String ReturnBook(Borrow borrow){
        System.out.println(borrow);
        if (borrow.getReturn().equals("N"))
        {
            int i = borrowService.DeleteBorrowByID(borrow.getID());
            if (i>0)
            {
                int j = borrowService.UpdateBookNumber(guestService.FindBookByID(borrow.getBookID()).getNumber()+1);
                if (j>0)    return "return success";
                else return "something wrong";
            }
            else return "something wrong";
        }
        else return "return false";
    }
}
